<template>
  <div>
    <div v-if="show" class="cus">
      <div class="cus_box" style="width:1200px;">
        <div class="title">
          <span>已关联设备</span>
          <i @click="show = false" class="el-icon-close"></i>
        </div>
        <div class="mt10">
          <el-button type="primary" @click="add">Add设备</el-button>
        </div>
        <div class="content">
          <el-table
            :data="tableData"
            height="500"
            border
            style="width: 100%"
            align="center"
          >
            <el-table-column
              align="center"
              prop="id"
              label="ID"
              width="180"
            ></el-table-column>
            <el-table-column
              align="center"
              prop="sn"
              label="机柜编号"
              width="180"
            ></el-table-column>
            <el-table-column
              align="center"
              prop="deviceType"
              label="设备Type"
              width="180"
            >
              <template slot-scope="scope">
                {{
                  scope.row["deviceType"] == 1
                    ? "换电柜"
                    : scope.row["deviceType"] == 2
                    ? "电动车"
                    : scope.row["deviceType"] == 3
                    ? "Charging Cabinet"
                    : "--"
                }}
              </template>
            </el-table-column>
            <el-table-column align="center" label="QR Code">
              <template slot-scope="scope">
                <el-popover placement="right" trigger="hover">
                  <el-image
                    style="width:300px;height:300px;"
                    :src="baseUrl + scope.row.qrCode"
                  >
                  </el-image>
                  <el-image
                    slot="reference"
                    :src="baseUrl + scope.row.qrCode"
                  ></el-image>
                </el-popover>
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              prop="deviceName"
              label="机柜Name"
            ></el-table-column>
            <el-table-column
              align="center"
              prop="address"
              label="地址"
            ></el-table-column>
            <el-table-column
              align="center"
              prop="contacts"
              label="Agent"
            ></el-table-column>
            <el-table-column
              align="center"
              prop="deviceStatus"
              label="机柜Status"
              width="140px"
            >
              <!-- 设备Status：1：（Online）运行中 2: Offline 3: 设备故障 -->
              <template slot-scope="scope">
                <span
                  :class="
                    scope.row['deviceStatus'] == 1
                      ? 'success'
                      : scope.row['deviceStatus'] == 2
                      ? 'c9'
                      : scope.row['deviceStatus'] == 3
                      ? 'danger'
                      : ''
                  "
                >
                  {{
                    scope.row["deviceStatus"] == 1
                      ? "（Online）运行中"
                      : scope.row["deviceStatus"] == 2
                      ? "Offline"
                      : scope.row["deviceStatus"] == 3
                      ? "设备故障"
                      : "--"
                  }}
                </span>
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              prop="ctime"
              label="Creation Time"
              width="160"
            ></el-table-column>
            <el-table-column align="center" label="Operations">
              <template slot-scope="scope">
                <el-button @click="advertList_cancel(scope.row)" type="text"
                  >Cancel关联</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
    </div>

    <add ref="add" v-on:add_call="add_call" :addList="tableData"></add>
  </div>
</template>

<script>
import add from "./add2";
export default {
  name: "Related_advertising",
  components: {
    add
  },
  data() {
    return {
      show: false,
      row: {},
      tableData: []
    };
  },
  mounted() {},
  methods: {
    async getData() {
      const res = await this.apis.advertDevice_deviceList({
        pageNum: 1,
        pageSize: 100000,
        abId: this.row.id
      });
      const list = res.rows || [];
      this.tableData = list;
    },
    fun_show(row) {
      this.row = row;
      this.show = true;

      this.getData();
    },
    add() {
      this.$refs.add.fun_show(this.row);
    },
    add_call() {
      this.getData();
    },
    advertList_cancel(row) {
      this.$confirm("Confirm要Cancel关联吗?", "Hint", {
        confirmButtonText: "Confirm",
        cancelButtonText: "Cancel",
        type: "warning"
      })
        .then(async () => {
          const res = await this.apis.advertDevice_cancel({
            abId: this.row.id,
            deviceId: row.id
          });
          this.$message({
            type: "success",
            message: "CancelSuccess!"
          });

          this.getData();
        })
        .catch(() => {});
    }
  }
};
</script>

<style scoped lang="scss"></style>
